package com.lanou3g.loginandregister;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by dllo on 18/4/10.
 */
public class Util {

    ArrayList<User> userArrayList = new ArrayList<>();



    public ArrayList<User> getUserArrayList() {
        return userArrayList;
    }
    public void setUserArrayList(ArrayList<User> userArrayList) {
        this.userArrayList = userArrayList;
    }

    public boolean login(ArrayList<User> user){

            System.out.println("请输入用户名");
            Scanner scanner = new Scanner(System.in);
            String username = scanner.nextLine();
            System.out.println("请输入密码");
            String pwd = scanner.nextLine();
            boolean usernameRight =false;
            boolean pwdRight =false;

           for (int i = 0; i < user.size(); i++) {

                usernameRight =username.equals(user.get(i).getName());
                pwdRight = pwd.equals(user.get(i).getPassword());

               if (usernameRight&&pwdRight){

                   return true;
               }
            }

        return false;

    }

    public ArrayList<User> register(){

        User user = new User();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入用户名");
            String username = scanner.nextLine();

            boolean isStartWithNum = false;
            for (int i = 0; i < 10; i++) {
                if (username.startsWith(String.valueOf(i))){
                    isStartWithNum = true;
                }
            }
            if (!isStartWithNum){
                user.setName(username);
                break;
            }else{
                System.out.println("用户名不能以数字开头");
            }
        }

        String pwd;
        while (true){
            System.out.println("请输入密码:");
            pwd = scanner.nextLine();
            int pwdLength = pwd.length();

            //判断密码是否8位以上

            if(pwdLength < 8){
                System.out.println("密码要在八位以上");
                continue;
            }

            //判断是否包含数字
            boolean isContainNum = false;
            for (int i = 0; i < 10; i++) {
                String letter = String.valueOf(i);
                if (pwd.contains(letter)){
                    isContainNum = true;
                }
            }
            if (!isContainNum){
                System.out.println("密码需包含数字");
                continue;
            }


            //判断是否包含大写字母
            boolean isContainUpperCase = false;
            for (char c = 'A'; c <= 'Z';c++ ){
                String letter = String.valueOf(c);
                if (pwd.contains(letter)){
                    isContainUpperCase = true;
                }
            }

            if (!isContainUpperCase){
                System.out.println("密码需包含大写字母");
                continue;
            }

            //判断是否包含小写字母
            boolean isContainLowerCase = false;
            for (char c = 'a'; c<= 'z';c++){
                String letter = String.valueOf(c);
                if (pwd.contains(letter)){
                    isContainLowerCase = true;
                }
            }

            if (!isContainLowerCase){
                System.out.println("密码需包含小写字母");
                continue;
            }

            break;
        }

        //确认密码
        System.out.println("请输入确认密码");
        String confirmPwd = scanner.nextLine();
        if (confirmPwd.equals(pwd)){
            System.out.println("密码一致");
            user.setPassword(pwd);
        }

        System.out.println("恭喜你注册成功");
        userArrayList.add(user);
        return userArrayList;
    }



}
